#include "dbutil.h"

DBUtil::DBUtil(DBInfo *dbInfo)
{
   db = QSqlDatabase::addDatabase("QMYSQL");
   db.setDatabaseName(dbInfo->dbname);
   db.setHostName(dbInfo->hostname);
   db.setPort(dbInfo->port);
   db.setUserName(dbInfo->username);
   db.setPassword(dbInfo->password);
}
int DBUtil::connect()
{
    return db.open();
}

QList<QMap<QString,QVariant>> DBUtil::query(QString table,QString filterStr)
{
    QList<QMap<QString,QVariant>> list;
    QSqlTableModel *model = new QSqlTableModel(nullptr,db);
    model->setTable(table);
    model->setFilter(filterStr);
    model->select();
    for(int i=0;i<model->rowCount();i++)
    {
        QMap<QString,QVariant> map;
        QSqlRecord record = model->record(i);
        for(int j=0;j<record.count();j++)
        {
            map.insert(record.fieldName(j),record.value(j));
        }
        list.append(map);
    }
    return list;
}

void DBUtil::insert(QList<QMap<QString,QObject>> data)
{

}
